package flib;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PImage;
import psd.model.Layer;
import psd.model.Psd;

public class PsdOpener extends PApplet {
public void setup(){
	setupPSD();
}
Psd psd;
BufferedImage bimg;
void setupPSD(){
	File folder=new File("/Users/administrator/projekte/Doktor/References/PDF/Das Grundrisswerk scan/plans");
	ArrayList<File> files=listFilesFromFolder(folder,".psd");
	for (int i=0;i<files.size();i++){
		println("i:"+i);
	File file=files.get(i);
	String fileName=file.getName();
	
	fileName=fileName.replace(".psd", "");
	try {
		 psd=new Psd(file);
	System.out.println("layers: "+psd.getLayersCount());
	int h=psd.getHeight();
	int w=psd.getWidth();
	for (int ii=0;ii<psd.getLayersCount();ii++){
		Layer layer=psd.getLayer(ii);
		System.out.println("layers: "+layer);
		bimg=layer.getImage();
		int lx=layer.getX();
		int ly=layer.getY();
		int lx2=lx+layer.getWidth();
		int ly2=ly+layer.getHeight();
		String name=layer.toString();
		if (!name.equals("Background")){
			String namePlan=fileName+name+".png";
			  PImage img=new PImage(w,h,PConstants.RGB);
			  	//bimg.getr
			  int offset=ly*w+lx;
			  int index=0;
			  		for (int y=0;y<h;y++){
			  			for (int x=0;x<w;x++){
			  				if (x>=lx&&y>=ly&&x<lx2&&y<ly2){
			  						int col=bimg.getRGB(x-lx, y-ly);
			  						img.pixels[index]=col;
			  				}
			  				else{
			  					img.pixels[index]=color(255);
			  				}
			  			index++;
				  	}
			  	}
			   // bimg.getRGB(0, 0, bimg.getWidth(), bimg.getHeight(), img.pixels, offset, img.width);
			    for (int iii=0;iii<img.pixels.length;iii++){
			    	float red=red(img.pixels[iii]);
		    		 float alpha=alpha(img.pixels[iii]);
		    		 if (alpha<100){
		    			 img.pixels[iii]=color(255);
		    		 }
		    		 else {
		    			 img.pixels[iii]=color(red);
		    		 }
			    }
			    img.updatePixels();
			img.save("/Users/administrator/projekte/Doktor/References/PDF/Das Grundrisswerk scan/png/"+namePlan);
			//ImageIO.write(bimg,"PNG",new File("/Users/administrator/projekte/Doktor/References/PDF/Das Grundrisswerk scan/png/"+namePlan));
		}
		
		bimg.flush();
		bimg=null;
		img=null;
		System.gc();
	}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
}
PImage img;
PImage smallimg;
void setupPreviews(){
	File folder=new File("/Users/administrator/Desktop/plans/");
	ArrayList<File> files=listFilesFromFolder(folder,".png");
	for (int i=0;i<files.size();i++){
		File file=files.get(i);
		println(file.getName());
		img=loadImage(file.getAbsolutePath());
		smallimg=new PImage(img.width/4,  img.height/4);
		smallimg.copy(img,0, 0, img.width, img.height, 0, 0, img.width/4,  img.height/4);
		smallimg.save("/Users/administrator/Desktop/plansSmall/"+file.getName());
	}
}

public static ArrayList<File> listFilesFromFolder(final File folder,String fileEnd) {
	ArrayList<File> files=new ArrayList<File>();
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isFile()) {
         String  temp = fileEntry.getName();
         if ( temp.toLowerCase().endsWith(fileEnd)){
        	 files.add(fileEntry);
         }
        }
    }
    return files;
  }
}
